package org.bigdata.zc.controller;

import lombok.extern.slf4j.Slf4j;
import org.bigdata.zc.mongo.CupWin;
import org.bigdata.zc.mongo.TeamDataWin;
import org.bigdata.zc.mongo.TeamWin;
import org.bigdata.zc.mongo.repository.CupRepository;
import org.bigdata.zc.mongo.repository.TeamRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Stephen on 2017/11/1.
 */
@Controller
@Slf4j
public class PageController {

    @Autowired
    CupRepository cupRepository;

    @Autowired
    TeamRepository teamRepository;


    /**
     * 联盟首页
     * @return
     */
    @RequestMapping(value = "/")
    public String cupIndex(HttpServletRequest request, Model model){
        List<CupWin> cupWins = cupRepository.findAll();
        model.addAttribute("cupList",cupWins);
        return "cupIndex";
    }

    /**
     * 我的算法
     * @return
     */
    @RequestMapping(value = "/myCalculation")
    public String myCalculation(HttpServletRequest request, Model model){
        List<CupWin> cupWins = cupRepository.findAll();
        model.addAttribute("cupList",cupWins);
        return "myCalculation";
    }

    /**
     * 我的预测
     * @return
     */
    @RequestMapping(value = "/myForecast")
    public String myForecast(HttpServletRequest request, Model model){
        List<CupWin> cupWins = cupRepository.findAll();
        model.addAttribute("cupList",cupWins);
        return "myForecast";
    }

    /**
     * 球队预测
     * @return
     */
    @RequestMapping(value = "/teamBattle")
    public String teamBattle(HttpServletRequest request, Model model,@RequestParam(value="cupId", required=true) String cupId,@RequestParam(value="hostId", required=true) String hostId,@RequestParam(value="visitorId", required=true) String visitorId){
        //获取两只球队的所有对阵数据（直接取数据库）
        //获取联盟所有球队的对阵数据（直接取数据库）
        //根据算法计算出胜率情况
        List<TeamWin> teamWins = teamRepository.findByCupNo(cupId);
        TeamWin hostTeam = teamRepository.findByTeamIdAndCupNo(hostId,cupId);
        TeamWin visitorTeam = teamRepository.findByTeamIdAndCupNo(visitorId,cupId);
        model.addAttribute("teamWins",teamWins);
        model.addAttribute("hostTeam",hostTeam);
        model.addAttribute("visitorTeam",visitorTeam);
        return "teamBattle";
    }

}
